package com.ym.b2b.order.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.order.service.EmSOrderConfirmItemService;
import com.ym.b2b.order.sqlbean.EmSOrderConfirmItem;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

@Controller
@RequestMapping("/trade/item")
public class ManagerEmSOrderConfirmItemController extends BaseController {

    @Autowired
    private EmSOrderConfirmItemService emSOrderConfirmItemService;

    @RequestMapping(value = "searchEmSOrderConfirmItem.do")
    @ResponseBody
    public Result searchEmSOrderConfirmItem(EmSOrderConfirmItem emSOrderConfirmItem, HttpServletRequest req) {

        Result ret = new Result();
        WebTokenUser tokenUser = getWebTokenUser(req);
        if (tokenUser != null) {
            emSOrderConfirmItem.setUser(tokenUser);
        }
        try {
            ret = emSOrderConfirmItemService.deleteEmSOrderConfirmItem(emSOrderConfirmItem,req);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;

    }

    @RequestMapping(value = "searchGroupEmSOrderConfirmItem.do")
    @ResponseBody
    public Result searchGroupEmSOrderConfirmItem(EmSOrderConfirmItem emSOrderConfirmItem, HttpServletRequest req) {

        Result ret = new Result();
        WebTokenUser tokenUser = getWebTokenUser(req);
        if (tokenUser != null) {
            emSOrderConfirmItem.setUser(tokenUser);
        }
        try {
            ret = emSOrderConfirmItemService.deleteEmSOrderConfirmItem(emSOrderConfirmItem,req);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;

    }

    /**
     * 销售明细
     * 
     * @Title: searchEmSOrderConfirmItem
     * @Description: TODO
     * @Param @param emSOrderConfirmItem
     * @Param @param req
     * @Param @return
     * @Return Result
     * @Throws
     */
    @RequestMapping(value = "searchPageEmSOrderConfirmItem.do")
    @ResponseBody
    public Result searchPageEmSOrderConfirmItem(EmSOrderConfirmItem emSOrderConfirmItem,
                                                Page<EmSOrderConfirmItem> page, HttpServletRequest req) {
        Result ret = new Result();
        try {

            ret = emSOrderConfirmItemService.searchPageEmSOrderConfirmItem(emSOrderConfirmItem, page);
        } catch (Exception e) {
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;

    }

    /**
     * 可退货申请列表
     * 
     * @Title: searchPageOrderItemReturn
     * @Throws
     */

    @RequestMapping("searchPageOrderItemReturn.do")
    @ResponseBody
    public Result searchPageOrderItemReturn(EmSOrderConfirmItem emSOrderConfirmItem, Page<EmSOrderConfirmItem> page,
                                            HttpServletRequest req) {
        Result result = new Result();
        try {
            page = emSOrderConfirmItemService.searchPageEmSOrderConfirmItemReturn(emSOrderConfirmItem, page);
            result.setResultPage(page);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 导出退货列表
    @RequestMapping(value = "searchPageOrderItemReturnExcel.do")
    @ResponseBody
    public Result searchPageOrderItemReturnExcel(EmSOrderConfirmItem emSOrderConfirmItem,
                                                 Page<EmSOrderConfirmItem> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            page = emSOrderConfirmItemService.searchPageEmSOrderConfirmItemReturn(emSOrderConfirmItem, page);
            ret.setResultPage(page);
            ExcelUtil<EmSOrderConfirmItem> excelUtil = new ExcelUtil<EmSOrderConfirmItem>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("orderReturnListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    /**
     * 申请退货操作
     * 
     * @Title: updateConfirmItemForIsReturn
     * @Throws
     */
    @RequestMapping("updateConfirmItemForIsReturn.do")
    @ResponseBody
    public Result updateConfirmItemForIsReturn(EmSOrderConfirmItem emSOrderConfirmItem, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emSOrderConfirmItemService.updateConfirmItemForIsReturn(emSOrderConfirmItem);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * 批量申请退货操作
     * 
     * @Title: updateMultipleConfirmItemForIsReturns
     * @Throws
     */
    @RequestMapping("updateMultipleConfirmItemForIsReturns.do")
    @ResponseBody
    public Result updateMultipleConfirmItemForIsReturns(EmSOrderConfirmItem emSOrderConfirmItem, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emSOrderConfirmItemService.updateMultipleConfirmItemForIsReturns(emSOrderConfirmItem);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

}
